import { defineConfig } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import vue from "@vitejs/plugin-vue";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

import path from "path";

const pathResolve = (pathStr) => {
  return path.resolve(__dirname, pathStr);
};
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹(路径为存放所有svg图标的文件夹不单个svg图标)
      // iconDirs: [pathResolve("./src/assets/svgs")],
      iconDirs: [path.resolve(process.cwd(), "src/assets/svgs")],
      // 指定symbolId格式
      symbolId: "icon-[dir]-[name]",
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  base: "/", //打包后，访问的路径环境变量
  resolve: {
    alias: {
      "@": pathResolve("./src"),
    },
  },
  server: {
    port: 8080, // 端口号
    historyApiFallback: true, // 支持 history 模式
    proxy: {
      "/api": {
        target: "http://localhost:3300", // 目标地址
        changeOrigin: true, // 是否跨域
        // secure: false, // 忽略https证书错误
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
});
